﻿using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using MDK.Common;
using MDK.Device;
using MDK.Device.Devices;
using MDK.Model.Models;
using MDK.WebApi;
using MDK.Winform.UserControl.Config;
using MDK.Winform;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Json;
using System.Windows.Forms;
using MDK.Common.Helper;
using System.Windows.Threading;

namespace MDK.Winform
{
    public partial class Index : XtraForm
    {
        private WebApiProxy proxy = new WebApiProxy();
        private IdentificationCamera Identification;
        private string  LoginType = System.Configuration.ConfigurationSettings.AppSettings["PcType"].ToString();
        /// <summary>
        /// 冷藏温度监控定时器
        /// </summary>
        DispatcherTimer _timer = new DispatcherTimer();
        public Index()
        {
            InitializeComponent();
        }

        #region 无边框移动
        //定义无边框窗体Form  
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        private void tableLayoutPanel_top_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);//*********************调用移动无窗体控件函数  
        }

        private void Index_Paint(object sender, PaintEventArgs e)
        {
            Rectangle myRectangle = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
            ControlPaint.DrawBorder(e.Graphics, myRectangle,
                    Color.Black, 1, ButtonBorderStyle.Solid,
                    Color.Black, 1, ButtonBorderStyle.Solid,
                    Color.Black, 1, ButtonBorderStyle.Solid,
                    Color.Black, 1, ButtonBorderStyle.Solid);
        }

        #endregion

        #region 窗体加载事件
        private void Index_Load(object sender, EventArgs e)
        {
            Init();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            App.LoadSystemSettings();
            InitControl();
            InitProxy();
            InitDisplay();
        }

        /// <summary>
        /// 初始化控件显示
        /// </summary>
        private void InitControl()
        {
            switch (LoginType)
            {
                case "ATM":
                    lbTitle.Text = "南昌血液中心-ATM自助交接机";
                    break;
                case "Pre":
                    lbTitle.Text = "南昌血液中心-前置处理设备";
                    break;
                case "Behind2":
                    lbTitle.Text = "南昌血液中心-后置处理设备";
                    break;
            }
            btnATM.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            btnPre.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            btnBehindPro.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            btnBehindProTwo.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            ribbonPageSys.Visible = false;
            ribbonPageDebug.Visible = false;
            ribbonPageControl.Visible = false;
            ribbonPageGroupPosition.Visible = false;
            xtraTabControl.TabPages.Clear();
        }

        /// <summary>
        /// 初始化代理
        /// </summary>
        private void InitProxy()
        {
            switch (LoginType)
            {
                case "ATM":
                    proxy.Start(App.systemSetting["ATMWebApiProxyPort"]);
                    break;
                case "Pre":
                    proxy.Start(App.systemSetting["FrontWebApiProxyPort"]);  //启动服务
                    break;
            }
        }

        /// <summary>
        /// 根据配置显示界面
        /// </summary>
        private void InitDisplay()
        {
            switch (LoginType)
            {
                case "ATM":
                frmLogin frm = new frmLogin();
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    btnATM.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    btnPre.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    btnBehindPro.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    btnBehindProTwo.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    ribbonPageSys.Visible = false;
                    ribbonPageDebug.Visible = false;
                    ribbonPageControl.Visible = true;
                    AddToTabControl("ATM", "MDK.Winform.ucATMControl");
                }
                    break;
                case "Pre":
                    btnATM.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    btnPre.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    btnBehindPro.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    btnBehindProTwo.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    ribbonPageSys.Visible = true;
                    ribbonPageControl.Visible = true;
                    ribbonPageGroupPosition.Visible = true;
                    picclose.Visible = false;
                    AddToTabControl("前处理", "MDK.Winform.ucPreControl");
                    App.StartFlow();
                    _timer.Tick += new EventHandler(TemperatureEven);
                    _timer.Interval = TimeSpan.FromSeconds(1800);   //设置刷新的间隔时间
                    _timer.Start();
                    break;
                case "Behind2":
                    btnATM.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    btnPre.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    btnBehindPro.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    btnBehindProTwo.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    ribbonPageSys.Visible = false;
                    ribbonPageDebug.Visible = false;
                    ribbonPageControl.Visible = true;
                    picclose.Visible = false;
                    AddToTabControl("后处理2", "MDK.Winform.ucBehindControlTwo");
                    break;
            }
        }

        private void TemperatureEven(object sender, EventArgs e)
        {
            var item = App.flow.SysModel.Freezer.FreezerController;//获取冷藏温度对象
            HandoverData.SandKeepTemperatureData(EnumCmdCode.StorageTemperature, ((MitsubishiPlc)item.Device.PLC).PlcIP, "FreezerController", "Temperature", (int)item.GetTemperatures()[0], (int)item.LowerLimit, (int)item.UpperLimit);
        }

        private IdentificationCamera GetIndentification()
        {
            string fp = Application.StartupPath + "\\IdentificationCamera.json";
            if (File.Exists(fp))
            {
                return Identification = JsonConvert.DeserializeObject<IdentificationCamera>(File.ReadAllText(fp));  // 尖括号<>中填入对象的类名 
            }
            return null;
        }
        #endregion

        private void AddToTabControl(string text, string controlAddress, bool autoSize = true)
        {
            if (string.IsNullOrEmpty(controlAddress))
                return;
            //判断是否存在
            foreach (XtraTabPage tabPages in xtraTabControl.TabPages)
            {
                if (tabPages.Text == text)
                {
                    xtraTabControl.SelectedTabPage = tabPages;
                    return;
                }
            }

            string[] strArray = controlAddress.Split(new char[] { ';' });

            Assembly assembly = this.GetType().Assembly;
            XtraUserControl control =
                (XtraUserControl)assembly.CreateInstance(strArray[0],
                                                        false,
                                                        BindingFlags.Default,
                                                        null,
                                                        strArray.Length > 1 ? strArray.Skip(1).ToArray() : null,
                                                        null,
                                                        null);

            if (control == null)
            {
                return;
            }

            XtraTabPage tabPage = new XtraTabPage();
            if (autoSize)
            {
                control.Dock = DockStyle.Fill;
            }

            tabPage.Text = text;
            xtraTabControl.TabPages.Add(tabPage);
            control.Parent = tabPage;
            xtraTabControl.SelectedTabPage = tabPage;
        }

        private void _qqHyperlinkLabelControl_Click(object sender, EventArgs e)
        {
            Process.Start("tencent://message/?uin=772356002");
        }

        private void picclose_Click(object sender, EventArgs e)
        {
            Init();
        }

        #region  顶部菜单
        /// <summary>
        /// ATM
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnATM_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddToTabControl("ATM", "MDK.Winform.ucATMControl");
        }

        /// <summary>
        /// 前处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPre_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddToTabControl("前处理", "MDK.Winform.ucPreControl");
        }


        /// <summary>
        /// 后处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBehindPro_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddToTabControl("后处理1", "MDK.Winform.ucBehindControl");
        }

        /// <summary>
        /// 后处理2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBehindProTwo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddToTabControl("后处理2", "MDK.Winform.ucBehindControlTwo");
        }

        /// <summary>
        ///用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAccount_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddToTabControl("用户", "MDK.Winform.ucAccount");
        }

        /// <summary>
        /// 样本统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSamples_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddToTabControl("样本统计", "MDK.Winform.ucSample");
        }


        /// <summary>
        /// 挑选记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPick_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddToTabControl("挑选记录", "MDK.Winform.ucPick");
        }

        /// <summary>
        /// 冷藏机温度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnTemperature_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddToTabControl("冷藏机温度", "MDK.Winform.ucTemperature");
        }

        /// <summary>
        /// 系统设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnComSet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddToTabControl("系统设置", "MDK.Winform.ucSystemSet");
        }
        #endregion

        #region 弹出框
        private void btnATMDebug_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ATM modify = new ATM();
            modify.ShowDialog();
        }

        private void btnPreDebug_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Pre modify = new Pre();
            modify.ShowDialog();
        }

        private void btnBehindDebug_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Behind modify = new Behind();
            modify.ShowDialog();
        }

        private void btnColdDebug_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cold modify = new Cold();
            modify.ShowDialog();
        }

        private void btnCenDebug_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           
        }

        private void btnCentrifuge_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddToTabControl("离心配置", "MDK.Winform.ucCentrifugeSet");
          //  modify.ShowDialog();
        }

        private void btnPosintion_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmPositionConfig modify = new frmPositionConfig();
            modify.ShowDialog();
        }

        private void btnCen_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmCentrifugeConfig modify = new frmCentrifugeConfig();
            modify.ShowDialog();
        }

        private void btnBelt_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmBeltConfig modify = new frmBeltConfig();
            modify.ShowDialog();
        }

        private void btnSetPwd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmModifyPassword modify = new frmModifyPassword();
            modify.ShowDialog();
        }

        private void btnFront_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserControl.Config.Position front = new UserControl.Config.Position(true);
            front.ShowDialog();
        }

        private void btnBack_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserControl.Config.Position front = new UserControl.Config.Position(false);
            front.ShowDialog();
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserControl.Config.Camera ca = new UserControl.Config.Camera();
            ca.ShowDialog();
        }

        private void barBack2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserControl.Config.Position front = new UserControl.Config.Position(false, true);
            front.ShowDialog();
        }

        private void btnPosition_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
          
        }

        #endregion

        private void btnDeskPosition_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmDeskOperation frm = new frmDeskOperation();
            frm.ShowDialog();
        }

        private void btnFreezerOperation_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmFreezerOperation frm = new frmFreezerOperation();
            frm.ShowDialog();
        }

      
    }
}
